<template>
  <div class="mapWrapPicture">
    <div class="containerWrap" ref="mapScreenfull">
      <div class="infoButton">
        <i @click='screenfullMap' class="iconfont icon-quanping2"></i>
      </div>
      <div class="colorWrap">
        <div class="checkWrap" style="text-align: left;">
          <el-radio-group v-model="enterpriseNo" size="mini" @change="changeRadio()">
            <div v-for="(item,index) in companyInfos" :key="index" style="line-height: 35px;text-align: left;">
              <span class="companyList">
                <el-radio-button :label="item.enterpriseNo">{{item.enterpriseName}}</el-radio-button></span>
              <!--<span class="companyCount">{{item.count}}</span>-->
            </div>
          </el-radio-group>
        </div>
      </div>
      <div id="mapContainer"></div>
    </div>
  </div>
</template>

<script>

  export default {
    props: ["district", "areaInfo", "companyInfos"],
    data () {
      return {
        map: "",
        searchInfoWindow: "",
        enterpriseNo: null,
        mapInfoWindow: "",
        markers: [],
        mapClickMarker: "",
        ply: [],

      }

    },
    mounted(){
      this.getHotMap()
    },
    created(){

    },
    methods: {
      changeRadio(){
        this.$emit("changeCompany", this.enterpriseNo)
      },
      //百度提取区域经纬度
      getBoundary(areaPoints) {
        let _this = this, arr = []
        if (areaPoints && areaPoints.length > 2) {
          _this.map.clearOverlays();        //清除地图覆盖物
          areaPoints.forEach((item) => {
            arr.push(new BMap.Point(item.lng, item.lat))
          })
          //              Polyline  Polygon
          _this.ply = new BMap.Polyline(arr, {
            strokeColor: "#3975FE",    //边线颜色。
            fillColor: "white",      //填充颜色。当参数为空时，圆形将没有填充效果。
            strokeWeight: 1,       //边线的宽度，以像素为单位。
            strokeOpacity: 1,    //边线透明度，取值范围0 - 1。
            fillOpacity: 1,      //填充的透明度，取值范围0 - 1。
            strokeStyle: 'solid' //边线的样式，solid或dashed。
          });  //创建多边形

          _this.map.addOverlay(_this.ply);  //添加覆盖物
          _this.map.setViewport(_this.ply.getPath());    //调整视野
          /*setTimeout(() => {
            _this.addMarkes(_this.areaInfo)
          }, 100)*/

        }
      },

      getHotMap(){
        let _this = this
        _this.map = new BMap.Map("mapContainer", {
          enableMapClick: false,
          minZoom: 15,
          maxZoom: 19
        });          // 创建地图实例

        _this.map.setMapStyle({
          styleJson: [
            {
              "featureType": "poilabel",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            },
            {
              "featureType": "manmade",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            },
            {
              "featureType": "building",
              "elementType": "all",
              "stylers": {
                "visibility": "off"
              }
            }
          ]
        });

        _this.map.centerAndZoom(new BMap.Point(114.0661345267, 22.5485544122), 16);          // 初始化地图，设置中心点坐标和地图级别
        _this.map.enableScrollWheelZoom(); // 允许滚轮缩放

        _this.map.addControl(new BMap.OverviewMapControl());
        _this.map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
        this.map.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT}));
//        地图点击事件
        /*setTimeout(() => {
          _this.addMarkes(_this.areaInfo)
        }, 500)*/

      },

      addMarkes(dataInnfo){
        let _this = this
        _this.map.clearOverlays();
        _this.markers = []
        dataInnfo.forEach((item1) => {
          item1.forEach((item, index) => {

            let imgUrl = item.stationType == 1 ? "./static/images/public.png" : './static/images/subway.png'
            let htm = "<div style='color:#0082CB;width:80px;height:26px;position: absolute;background-color: rgba(0,0,0,0);'>"
              + "<img src=" + imgUrl + " />"
              + "<span style='position: absolute;left: 28px;top: 4px;font-size: 11px;'>" + item.totalCount + "</span></div>"

            let startIcon = new BMapLib.RichMarker(htm, new BMap.Point(item.bd_lng, item.bd_lat), {
              "anchor": new BMap.Size(-20, -26),
              "enableDragging": false
            });
            _this.map.addOverlay(startIcon)
            _this.addClickHandler(startIcon, item);

          })
        })
      },
      getEnterpriseName(enterpriseNo){
        let name = ""
        this.$store.state.companyInfos.forEach(item => {
          if (item.enterpriseNo == enterpriseNo) {
            name = item.enterpriseName
          }
        })
        return name
      },
      //覆盖物点击
      addClickHandler (marker, info) {
        let _this = this
        marker.addEventListener("click", function (e) {
            let point = new BMap.Point(info.bd_lng, info.bd_lat), content, contentIner = "<span> </span>",
              name = info.stationName

            if (info.enterpriseList && info.enterpriseList.length > 0) {
              info.enterpriseList.forEach(item => {
                let enterpriseName = _this.getEnterpriseName(item.enterpriseNo)
                contentIner += '<div style="padding: 0 10px;color: #50545D;">' + enterpriseName + '：' + item.count + '</div> '
              })
            }
            //创建检索信息窗口对象
            this.searchInfoWindow = null;
            if (_this.IEVersion() == -1) {
              content = '<div class="mapTopWrap" > <div class="mapBaseTitle" >车辆信息</div> <div style="float: left;line-height: 31px;border-left: 1px solid #EFEEF3; "> <div style="padding: 0 10px;color: #50545D;">停放总量：' + info.totalCount + '</div> ' +
                contentIner +
                '</div> </div> '
            } else {
              content = '<div class="mapTopWrap" > <div style="color: #3975FE;font-size: 12px;padding-left: 10px;padding-bottom: 5px;padding-top: 5px;" >车辆信息</div> <div style="float: left;line-height: 25px;"> ' +
                '<div style="padding: 0 10px;color: #50545D;">停放总量：' + info.totalCount + '</div> ' +
                contentIner +
                '</div> </div> '
            }

            _this.searchInfoWindow = new BMapLib.SearchInfoWindow(_this.map, content, {
              title: name,      //标题
              panel: "panel",         //检索结果面板
              enableAutoPan: true,     //自动平移
              offset: new BMap.Size(-20, -20),
              searchTypes: []
            });
            _this.marker = new BMap.Marker(point); //创建marker对象

            _this.searchInfoWindow.open(_this.marker)

          }, true
        )
      },

      screenfullMap(){
        if (this.screenfull.enabled) {
          this.screenfull.toggle(this.$refs.mapScreenfull);
        }
      },

    },
    watch: {
      /*areaInfo(){
        this.addMarkes(this.areaInfo)
      },*/

    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .mapWrapPicture
    width: 100%;
    height 100%
    &:hover .infoButton
      opacity: 1 !important;
    .containerWrap
      height: 100%;
      width: 100%;
      margin 0px auto;
      position: relative;
      .infoButton
        height 31px
        font-size: 16px;
        color: #6A6FFC;
        position: absolute;
        top: 10px;
        right: 10px;
        z-index 5
        opacity: 0;
        .iconfont
          display: inline-block;
          font-size: 18px;
          cursor pointer
      .colorWrap
        position: absolute;
        left 10px;
        top: 10px;
        background-color rgba(255, 255, 255, 0.7)
        line-height 20px
        padding 10px
        border-radius 5px
        z-index: 5;
        color: black;
        .checkWrap
          line-height 35px;
          .el-radio-button
            .el-radio-button__inner
              border-radius 10px
              padding 5px 15px
          .is-active
            .el-radio-button__inner
              background-color: #3975FE
              border-radius 10px
              border-color #3975FE
          .companyCount
            font-size: 14px;
            color: #3975FE;
            font-weight: bold;
            line-height: 28px;
            vertical-align: middle;
        .colorlist
          margin-top 12px
          margin-right 10px
          float left;
          display: inline-block;
          width: 10px;
          height 10px;
          border-radius: 50%;
        .companyList
          display: inline-block;
          width: 60px;
          text-align: left;
      #mapContainer
        height: 100%;
        width: 100%;
    .mapTopWrap
      background-color: white;
      overflow: hidden;
      min-height 90px;
      /*width 240px*/
      .mapBaseTitle
        color: #3975FE;
        font-size: 12px;
        writing-mode: vertical-lr;
        letter-spacing: 5px;
        float: left;
        padding: 11px;

    .BMapLib_SearchInfoWindow
      width 200px !important
      &::after
        content: ""
        position: absolute;
        background-color: white;
        width: 100px;
        height: 3px;
        bottom: 0px;
        left: 50px;
        z-index 100

    .BMapLib_bubble_title {
      padding-left: 14px !important;
      font-size: 14px !important;
      font-weight: bold !important;
      background-color white
      border-radius 10px !important
      /*display none !important*/
    }

    .BMapLib_bubble_top, .BMapLib_bubble_close {
      border-radius 10px !important
      border none !important
      background-color #f9f9f9 !important
    }

    #BMapLib_SearchInfoWindow0, .BMapLib_SearchInfoWindow {
      border-radius 10px !important
      /*min-width 200px !important*/
      box-shadow: 0px 0px 24px 0px rgba(0, 0, 0, 0.34) !important
      border none
    }

    .BMapLib_SearchInfoWindow .BMapLib_bubble_center {
      position static !important /*position absolute
      left 0
      top 31px*/
    }

    .BMapLib_bubble_content {
      min-height 100px !important
    }

</style>
